home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 21 / AACD 21.iso / AACD / Utilities / Ghostscript / src / gshsb.c < prev    next >
Encoding:
C/C++ Source or Header  |  2001-01-01  |  4.2 KB  |  166 lines

  1. /* Copyright (C) 1994, 1997, 1998 Aladdin Enterprises.  All rights reserved.
  2.   
  3.   This file is part of AFPL Ghostscript.
  4.   
  5.   AFPL Ghostscript is distributed with NO WARRANTY OF ANY KIND.  No author or
  6.   distributor accepts any responsibility for the consequences of using it, or
  7.   for whether it serves any particular purpose or works at all, unless he or
  8.   she says so in writing.  Refer to the Aladdin Free Public License (the
  9.   "License") for full details.
  10.   
  11.   Every copy of AFPL Ghostscript must include a copy of the License, normally
  12.   in a plain ASCII text file named PUBLIC.  The License grants you the right
  13.   to copy, modify and redistribute AFPL Ghostscript, but only under certain
  14.   conditions described in the License.  Among other things, the License
  15.   requires that the copyright notice and this notice be preserved on all
  16.   copies.
  17. */
  18.  
  19. /*$Id: gshsb.c,v 1.2 2000/09/19 19:00:29 lpd Exp $ */
  20. /* HSB color operators for Ghostscript library */
  21. #include "gx.h"
  22. #include "gscolor.h"
  23. #include "gshsb.h"        /* interface definition */
  24. #include "gxfrac.h"
  25.  
  26. /* Forward references */
  27. private void color_hsb_to_rgb(P4(floatp h, floatp s, floatp b, float rgb[3]));
  28. private void color_rgb_to_hsb(P4(floatp r, floatp g, floatp b, float hsb[3]));
  29.  
  30. /* Force a parameter into the range [0.0..1.0]. */
  31. #define force_unit(p) (p < 0.0 ? 0.0 : p > 1.0 ? 1.0 : p)
  32.  
  33. /* sethsbcolor */
  34. int
  35. gs_sethsbcolor(gs_state * pgs, floatp h, floatp s, floatp b)
  36. {
  37.     float rgb[3];
  38.  
  39.     color_hsb_to_rgb(force_unit(h), force_unit(s), force_unit(b), rgb);
  40.     return gs_setrgbcolor(pgs, rgb[0], rgb[1], rgb[2]);
  41. }
  42.  
  43. /* currenthsbcolor */
  44. int
  45. gs_currenthsbcolor(const gs_state * pgs, float pr3[3])
  46. {
  47.     float rgb[3];
  48.  
  49.     gs_currentrgbcolor(pgs, rgb);
  50.     color_rgb_to_hsb(rgb[0], rgb[1], rgb[2], pr3);
  51.     return 0;
  52. }
  53.  
  54. /* ------ Internal routines ------ */
  55.  
  56. /* Note: the color model conversion algorithms are taken from */
  57. /* Rogers, Procedural Elements for Computer Graphics, pp. 401-403. */
  58.  
  59. /* Convert RGB to HSB. */
  60. private void
  61. color_rgb_to_hsb(floatp r, floatp g, floatp b, float hsb[3])
  62. {
  63.     frac red = float2frac(r), green = float2frac(g), blue = float2frac(b);
  64.  
  65. #define rhue hsb[0]
  66. #define rsat hsb[1]
  67. #define rbri hsb[2]
  68.     if (red == green && green == blue) {
  69.     rhue = 0;        /* arbitrary */
  70.     rsat = 0;
  71.     rbri = r;        /* pick any one */
  72.     } else {            /* Convert rgb to hsb */
  73.     frac V, Temp, diff;
  74.     long H;
  75.  
  76.     V = (red > green ? red : green);
  77.     if (blue > V)
  78.         V = blue;
  79.     Temp = (red > green ? green : red);
  80.     if (blue < Temp)
  81.         Temp = blue;
  82.     diff = V - Temp;
  83.     if (V == red)
  84.         H = (green - blue) * frac_1_long / diff;
  85.     else if (V == green)
  86.         H = (blue - red) * frac_1_long / diff + 2 * frac_1_long;
  87.     else            /* V == blue */
  88.         H = (red - green) * frac_1_long / diff + 4 * frac_1_long;
  89.     if (H < 0)
  90.         H += 6 * frac_1_long;
  91.     rhue = H / (frac_1 * 6.0);
  92.     rsat = diff / (float)V;
  93.     rbri = frac2float(V);
  94.     }
  95. #undef rhue
  96. #undef rsat
  97. #undef rbri
  98. }
  99.  
  100. /* Convert HSB to RGB. */
  101. private void
  102. color_hsb_to_rgb(floatp hue, floatp saturation, floatp brightness, float rgb[3])
  103. {
  104.     if (saturation == 0) {
  105.     rgb[0] = rgb[1] = rgb[2] = brightness;
  106.     } else {            /* Convert hsb to rgb. */
  107.     /* We rely on the fact that the product of two */
  108.     /* fracs fits into an unsigned long. */
  109.     floatp h6 = hue * 6;
  110.     ulong V = float2frac(brightness);    /* force arithmetic to long */
  111.     frac S = float2frac(saturation);
  112.     int I = (int)h6;
  113.     ulong F = float2frac(h6 - I);    /* ditto */
  114.  
  115.     /* M = V*(1-S), N = V*(1-S*F), K = V*(1-S*(1-F)) = M-N+V */
  116.     frac M = V * (frac_1_long - S) / frac_1_long;
  117.     frac N = V * (frac_1_long - S * F / frac_1_long) / frac_1_long;
  118.     frac K = M - N + V;
  119.     frac R, G, B;
  120.  
  121.     switch (I) {
  122.         default:
  123.         R = V;
  124.         G = K;
  125.         B = M;
  126.         break;
  127.         case 1:
  128.         R = N;
  129.         G = V;
  130.         B = M;
  131.         break;
  132.         case 2:
  133.         R = M;
  134.         G = V;
  135.         B = K;
  136.         break;
  137.         case 3:
  138.         R = M;
  139.         G = N;
  140.         B = V;
  141.         break;
  142.         case 4:
  143.         R = K;
  144.         G = M;
  145.         B = V;
  146.         break;
  147.         case 5:
  148.         R = V;
  149.         G = M;
  150.         B = N;
  151.         break;
  152.     }
  153.     rgb[0] = frac2float(R);
  154.     rgb[1] = frac2float(G);
  155.     rgb[2] = frac2float(B);
  156. #ifdef DEBUG
  157.     if (gs_debug_c('c')) {
  158.         dlprintf7("[c]hsb(%g,%g,%g)->VSFI(%ld,%d,%ld,%d)->\n",
  159.               hue, saturation, brightness, V, S, F, I);
  160.         dlprintf6("   RGB(%d,%d,%d)->rgb(%g,%g,%g)\n",
  161.               R, G, B, rgb[0], rgb[1], rgb[2]);
  162.     }
  163. #endif
  164.     }
  165. }
  166.